package com.zork.cmdb.webapi.domain.mapper.impl;

import com.zork.cmdb.webapi.domain.dbo.ModuleDO;
import com.zork.cmdb.webapi.domain.dbo.ModuleRelationshipDO;
import com.zork.cmdb.webapi.domain.dbo.ModuleRelationshipGroupDO;
import com.zork.cmdb.webapi.domain.dbo.RelationshipTypeDO;
import com.zork.cmdb.webapi.domain.dto.RelationshipInstanceDTO;
import com.zork.cmdb.webapi.domain.mapper.CustomRelationshipInstanceMapper;
import com.zork.cmdb.webapi.domain.mapper.RelationshipInstanceMapper;
import com.zork.cmdb.webapi.domain.vo.RelationshipInstanceVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author chenjh
 * @date 2018/5/24
 * @description
 */
@Component
public class RelationshipInstanceMapperImpl implements CustomRelationshipInstanceMapper {

    private ConcurrentHashMap<Integer, ModuleDO> moduleMap = null;
    private ConcurrentHashMap<Integer, RelationshipTypeDO> relationshipTypeMap = null;
    private ConcurrentHashMap<Integer, ModuleRelationshipGroupDO> moduleRelationshipGroupMap = null;

    public Map<Integer, ModuleDO> getModuleMap() {
        return moduleMap;
    }

    @Autowired
    private com.zork.cmdb.webapi.mapper.cmdb.RelationshipInstanceMapper relationshipInstanceMapper;

    @Override
    public List<RelationshipInstanceDTO> getDTOList(List<ModuleRelationshipDO> moduleRelationshipDOList) {
        List<RelationshipInstanceDTO> relationshipInstanceDTOList = RelationshipInstanceMapper.MAPPER.getDTOList(moduleRelationshipDOList);
        for (RelationshipInstanceDTO relationshipInstance : relationshipInstanceDTOList) {
            if (moduleMap == null) {
                loadModuleMap();
            }
            ModuleDO selectedModule = moduleMap.get(relationshipInstance.getSelectedModuleID());
            ModuleDO relationModule = moduleMap.get(relationshipInstance.getRalationshipModuleID());
            if (selectedModule == null || relationModule == null) {
                loadModuleMap();
                selectedModule = moduleMap.get(relationshipInstance.getSelectedModuleID());
                relationModule = moduleMap.get(relationshipInstance.getRalationshipModuleID());
            }
            if (relationshipTypeMap == null) {
                loadRelationshipTypeMap();
            }
            RelationshipTypeDO relationshipType = relationshipTypeMap.get(relationshipInstance.getRalationshipTypeID());
            if (relationshipType == null) {
                loadRelationshipTypeMap();
                relationshipType = relationshipTypeMap.get(relationshipInstance.getRalationshipTypeID());
            }
            if (moduleRelationshipGroupMap == null) {
                loadModuleRelationshipGroupMap();
            }
            ModuleRelationshipGroupDO relationshipGroup = moduleRelationshipGroupMap.get(relationshipInstance.getRalationshipGroupID());
            if (relationshipGroup == null) {
                loadModuleRelationshipGroupMap();
                relationshipGroup = moduleRelationshipGroupMap.get(relationshipInstance.getRalationshipGroupID());
            }
            relationshipInstance.setSelectedModule(selectedModule);
            relationshipInstance.setRalationshipModule(relationModule);
            relationshipInstance.setRalationshipType(relationshipType);
            relationshipInstance.setRalationshipGroup(relationshipGroup);
        }
        return relationshipInstanceDTOList;
    }

    @Override
    public List<RelationshipInstanceVO> getVOList(List<RelationshipInstanceDTO> relationshipInstanceDTOList) {
        List<RelationshipInstanceVO> relationshipInstanceList = RelationshipInstanceMapper.MAPPER.getVOList(relationshipInstanceDTOList);
        for (RelationshipInstanceVO relationshipInstance : relationshipInstanceList) {
            switch (relationshipInstance.getLimitCodition()) {
                case 0 :
                    relationshipInstance.setLimitConditionName("一对一");
                    break;
                case 1:
                    relationshipInstance.setLimitConditionName("一对多");
                    break;
                case 2:
                    relationshipInstance.setLimitConditionName("多对一");
                    break;
                case 3:
                    relationshipInstance.setLimitConditionName("多对多");
                    break;
                default:
                    break;
            }
            switch (relationshipInstance.getRalationshipLevel()) {
                case 0 :
                    relationshipInstance.setRalationshipLevelName("上层关系");
                    break;
                case 1 :
                    relationshipInstance.setRalationshipLevelName("上层关系");
                    break;
                default :
                    break;
            }
        }
        return relationshipInstanceList;
    }

    public void loadModuleMap () {
        moduleMap = new ConcurrentHashMap<>();
        List<ModuleDO> moduleDOList = relationshipInstanceMapper.listModule();
        for (ModuleDO module : moduleDOList) {
            moduleMap.put(module.getModuleID(), module);
        }
    }

    private void loadRelationshipTypeMap () {
        relationshipTypeMap = new ConcurrentHashMap<>();
        List<RelationshipTypeDO> relationshipTypeDOList = relationshipInstanceMapper.listRelationshipType();
        for (RelationshipTypeDO relationshipType : relationshipTypeDOList) {
            relationshipTypeMap.put(relationshipType.getRalationshipTypeID(), relationshipType);
        }
    }

    private void loadModuleRelationshipGroupMap () {
        moduleRelationshipGroupMap = new ConcurrentHashMap<>();
        List<ModuleRelationshipGroupDO> moduleRelationshipGroupDOList = relationshipInstanceMapper.listModuleRelationshipGroup();
        for (ModuleRelationshipGroupDO moduleRelationshipGroup : moduleRelationshipGroupDOList) {
            moduleRelationshipGroupMap.put(moduleRelationshipGroup.getRalationshipGroupID(), moduleRelationshipGroup);
        }
    }
}
